package com.yanshibo.common_1902B.utils;

/*
 *字符串工具类
 *@auhtor yanshibo 
 *trim 去空格
 */
public class StringUtil {
	//字母的正则
	private static final String LETTER="[a-zA-Z]+";
	//手机号重则
	private static final String PHONE="^1[3|4|5|7|8]\\d{9}$";
	//邮箱正则
	private static final String EMAIL = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
	/*
	 * 判断字符串是否为空
	 * 
	 */
	public static boolean hasText(String str) {
		
		return str!=null && str.trim().length()>0;
	}
	/*
	 * 
	 * 2.判断字符串是否有值
	 */
	public static boolean hasLength(String str) {
		
		return str!=null && str.length()>0;
	}
	
	//反转字符串
	/*
	 * 
	 */
	public static String reverse(String str) {
		if(!hasText(str)) {
			return null;
		}
                   		//new StringBuffer(str).reverse().toString();
		return new StringBuffer(str).reverse().toString();
		
	}
	//4隐藏字符串
	public static String hiden(String str,Integer start,Integer end) {
		if(!hasText(str)) {
			return "传输数据不为空";
		}
	String before = str.substring(0, start);
	
	String hidden = str.substring(start, end);
	
	String after = str.substring(end);
	
	StringBuilder builder = new StringBuilder();
	for (int i = 0; i < hidden.length(); i++) {
		builder.append("*");
		
	}
	
		return before+builder.toString()+after;
		
	}
	//正则表达式
	//是否全为字母
	public static boolean isLetter(String str) {
		
		if(!hasText(str)) {
			return false;
			
		}
		
		//str.matches("[a-zA-z]+");
		return str.matches(LETTER);
	}
	/*
	 *手机号
	 * 
	 */
	public static boolean isPhone(String str) {
		if(!hasText(str)) {
			return false;
			
		}
		return str.matches(PHONE);
		
	}
	public static boolean isEmail(String str) {
		if(!hasText(str)) {
			return false;
			
		}
		return str.matches(EMAIL);
		
	}
	
}
